/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.LoanManager;

import gui.ItemManager.*;
import resource.Macro;
import utilities.DBConnector;
import utilities.Prompt;
import utilities.Validation;

/**
 *
 * @author jiaf
 */
public class LoanManager extends ItemManagerBase {
    /**
     * Creates new form ItemManager
     * @param dbSession
     */
    public LoanManager(DBConnector dbSession) {
        super(dbSession);
        initComponents();
        setTargetTable(itemTable);
        searchAvailableItems();
        returnItemButton.setEnabled(false);
        reminderButton.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        itemTable = new javax.swing.JTable();
        loanItemButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        itemSearchField = new javax.swing.JTextField();
        itemSearchOption = new javax.swing.JComboBox();
        returnItemButton = new javax.swing.JButton();
        itemStatusOption = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        reminderButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        reminder_email = new javax.swing.JTextField();
        email_password = new javax.swing.JPasswordField();
        remindAllButton = new javax.swing.JButton();
        priorDays = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        itemTable.setAutoCreateRowSorter(true);
        itemTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Call_No", "Type", "Name", "Author", "Publish_Year", "Publisher", "Category", "ISBN", "Register_Date", "Price", "Status", "Owner", "Return_Date", "Notes", "id"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        itemTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(itemTable);

        loanItemButton.setText("Loan Item");
        loanItemButton.setToolTipText("");
        loanItemButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loanItemButtonMouseClicked(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        itemSearchField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                itemSearchFieldKeyTyped(evt);
            }
        });

        itemSearchOption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Call No.", "ISBN" }));

        returnItemButton.setText("Return Item");
        returnItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnItemButtonActionPerformed(evt);
            }
        });

        itemStatusOption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "On Shelf", "On Loan" }));
        itemStatusOption.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemStatusOptionActionPerformed(evt);
            }
        });

        jLabel1.setText("Search By");

        reminderButton.setText("Send Reminder");
        reminderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reminderButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Source Email");

        jLabel3.setText("Password");

        reminder_email.setText("library.ccmc@gmail.com");

        remindAllButton.setText("Remind near expiry/expired");
        remindAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remindAllButtonActionPerformed(evt);
            }
        });

        priorDays.setModel(new javax.swing.SpinnerNumberModel(7, 1, 21, 1));

        jLabel4.setText("Prior Days");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(loanItemButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(returnItemButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(itemStatusOption, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(reminderButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addGap(27, 27, 27)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(reminder_email, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                            .addComponent(email_password))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(remindAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(priorDays))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(itemSearchOption, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(itemSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(loanItemButton)
                            .addComponent(itemSearchOption, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(itemSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(itemStatusOption, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(reminder_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(returnItemButton)
                                .addComponent(reminderButton)
                                .addComponent(cancelButton)
                                .addComponent(jLabel3)
                                .addComponent(email_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(priorDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(remindAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_cancelButtonMouseClicked

    private void loanItemButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loanItemButtonMouseClicked
        int row_num = itemTable.getSelectedRow();
        if (row_num != -1){
            LM_Edit bm_edit = new LM_Edit(getDBSession(), itemTable);
            bm_edit.setVisible(true);
        }
        else{
            Prompt.warning("No data selected");
        }
    }//GEN-LAST:event_loanItemButtonMouseClicked
    
    private void itemSearchFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_itemSearchFieldKeyTyped
        String searchKey = itemSearchField.getText();
        if(itemSearchOption.getSelectedItem().toString().equals(
                Macro.SEARCH_BY_CALL_NUMBER)){
            searchByCallNumber(searchKey);
        }
        else if (itemSearchOption.getSelectedItem().toString().equals(
                Macro.SEARCH_BY_ISBN)){
            searchByISBN(searchKey);
        }
    }//GEN-LAST:event_itemSearchFieldKeyTyped

    private void returnItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnItemButtonActionPerformed
        int row_num = itemTable.getSelectedRow();
        if (row_num != -1){
            ReturnItem rm_return =
                    new ReturnItem(getDBSession(), itemTable, row_num);
            rm_return.returnItem();
        }
        else{
            Prompt.warning("No data selected");
        }
    }//GEN-LAST:event_returnItemButtonActionPerformed

    private void itemStatusOptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemStatusOptionActionPerformed
        if (itemStatusOption.getSelectedIndex() == 0){
            searchAvailableItems();
            returnItemButton.setEnabled(false);
            loanItemButton.setEnabled(true);
            reminderButton.setEnabled(false);
        }
        else{
            searchLoanedItems();
            returnItemButton.setEnabled(true);
            loanItemButton.setEnabled(false);
            reminderButton.setEnabled(true);
        }
    }//GEN-LAST:event_itemStatusOptionActionPerformed
    
    private void reminderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reminderButtonActionPerformed
        int row_num = itemTable.getSelectedRow();
        if (row_num != -1){
            ReturnItem rm_return =
                    new ReturnItem(getDBSession(), itemTable, row_num);
            if (!Validation.empty("Email", reminder_email.getText()) &&
                    !Validation.empty("Password",
                            new String(email_password.getPassword()))){
                rm_return.sendReminder(reminder_email.getText(),
                        new String(email_password.getPassword()));
            }
        }
        else{
            Prompt.warning("No data selected");
        }
    }//GEN-LAST:event_reminderButtonActionPerformed

    private void remindAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remindAllButtonActionPerformed
        for (int i = 0; i < itemTable.getRowCount(); i++){
            ReturnItem rm_return =
                    new ReturnItem(getDBSession(), itemTable, i);
            if (rm_return.needToRemind((Integer) priorDays.getValue())){
                if (!Validation.empty("Email", reminder_email.getText()) &&
                        !Validation.empty("Password",
                                new String(email_password.getPassword()))){
                    rm_return.sendReminder(reminder_email.getText(),
                            new String(email_password.getPassword()));
                }
            }
        }
    }//GEN-LAST:event_remindAllButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPasswordField email_password;
    private javax.swing.JTextField itemSearchField;
    private javax.swing.JComboBox itemSearchOption;
    private javax.swing.JComboBox itemStatusOption;
    private javax.swing.JTable itemTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton loanItemButton;
    private javax.swing.JSpinner priorDays;
    private javax.swing.JButton remindAllButton;
    private javax.swing.JButton reminderButton;
    private javax.swing.JTextField reminder_email;
    private javax.swing.JButton returnItemButton;
    // End of variables declaration//GEN-END:variables
}
